home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 4 / Mac Giga-ROM 4.0 - 1993.toast / FILES / DEV / C-H / CMacPrimer.cpt / Reminder ƒ / Reminder.c < prev   
Encoding:
C/C++ Source or Header  |  1991-01-11  |  12.8 KB  |  583 lines  |  [TEXT/KAHL]

  1. #define BASE_RES_ID            400
  2. #define ABOUT_ALERT            401
  3. #define BAD_SYS_ALERT        402
  4. #define NIL_POINTER            0L
  5. #define MOVE_TO_FRONT        -1L
  6. #define REMOVE_ALL_EVENTS    0
  7.  
  8. #define MIN_SLEEP            0L
  9. #define NIL_MOUSE_REGION    0L
  10.  
  11. #define DRAG_THRESHOLD        30
  12.  
  13. #define SAVE_BUTTON            1
  14. #define CANCEL_BUTTON        2
  15. #define TIME_FIELD            4
  16. #define S_OR_M_FIELD        5
  17. #define SOUND_ON_BOX        6
  18. #define ICON_ON_BOX            7
  19. #define ALERT_ON_BOX        8
  20. #define SECS_RADIO            10
  21. #define MINS_RADIO            11
  22.  
  23. #define DEFAULT_SECS_ID        401
  24. #define DEFAULT_MINS_ID        402
  25.  
  26. #define ON                    1
  27. #define OFF                    0
  28.  
  29. #define SECONDS                0
  30. #define MINUTES                1
  31. #define SECONDS_PER_MINUTE    60
  32.  
  33. #define TOP                    25
  34. #define LEFT                12
  35.  
  36. #define MARK_APPLICATION    1
  37.  
  38. #define APPLE_MENU_ID        BASE_RES_ID
  39. #define FILE_MENU_ID        BASE_RES_ID+1
  40. #define ABOUT_ITEM_ID        1
  41.  
  42. #define CHANGE_ITEM            1
  43. #define START_STOP_ITEM        2
  44. #define KILL_ITEM            3
  45. #define QUIT_ITEM            4
  46.  
  47. #define SYS_VERSION            1
  48.  
  49. DialogPtr    gSettingsDialog;
  50. Rect        gDragRect;
  51. Boolean        gDone, gCounting, gNotify_set;
  52. char        gSeconds_or_minutes = SECONDS;
  53. StringHandle    gNotifyStrH, gDefaultSecsH, gDefaultMinsH;
  54. NMRec        gMyNMRec;
  55. MenuHandle    gAppleMenu, gFileMenu;
  56. EventRecord    gTheEvent;
  57.  
  58. struct
  59. {
  60.     Str255    timeString;
  61.     int     sound;
  62.     int     icon;
  63.     int     alert;
  64.     int        secsRadio;
  65.     int        minsRadio;
  66. }    savedSettings;
  67.  
  68.  
  69. /**************** main **************************/
  70.  
  71. main()
  72. {
  73.     ToolBoxInit();
  74.     if (Sys6OrLater())
  75.     {
  76.         DialogInit();
  77.         MenuBarInit();
  78.         SetUpDragRect();
  79.         NotifyInit();
  80.         MainLoop();
  81.     }
  82. }
  83.  
  84.  
  85. /********************* ToolBoxInit ******************/
  86.  
  87. ToolBoxInit()
  88. {
  89.     InitGraf(&thePort);
  90.     InitFonts();
  91.     FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
  92.     InitWindows();
  93.     InitMenus();
  94.     TEInit();
  95.     InitDialogs(NIL_POINTER);
  96.     InitCursor();
  97. }
  98.  
  99.  
  100. /********************* Sys6OrLater ******************/
  101.  
  102. int    Sys6OrLater()
  103. {
  104.     OSErr        status;
  105.     SysEnvRec    SysEnvData;
  106.     
  107.     status = SysEnvirons(SYS_VERSION, &SysEnvData);
  108.     if ((status != noErr)||(SysEnvData.systemVersion < 0x0600))
  109.     {
  110.         StopAlert(BAD_SYS_ALERT, NIL_POINTER);
  111.         return(FALSE);
  112.     }
  113.     else
  114.         return(TRUE);
  115. }
  116.  
  117. /******************** DialogInit **********************/
  118.  
  119. DialogInit()
  120. {
  121.     int        itemType;
  122.     Rect    itemRect;
  123.     Handle    itemHandle;
  124.     
  125.     gDefaultSecsH = GetString (DEFAULT_SECS_ID);
  126.     gDefaultMinsH = GetString (DEFAULT_MINS_ID);
  127.     
  128.     gSettingsDialog = GetNewDialog (BASE_RES_ID, NIL_POINTER, 
  129.                                             MOVE_TO_FRONT);
  130.     GetDItem (gSettingsDialog, SECS_RADIO, &itemType, &itemHandle,
  131.                                             &itemRect);
  132.     SetCtlValue (itemHandle, ON);
  133.     GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle, 
  134.                                             &itemRect);
  135.     SetCtlValue (itemHandle, ON);
  136.     GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle, 
  137.                                             &itemRect);
  138.     SetCtlValue (itemHandle, ON);
  139.     GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle, 
  140.                                             &itemRect);
  141.     SetCtlValue (itemHandle, ON);
  142. }
  143.  
  144. /************************ MenuBarInit ********************/
  145.  
  146. MenuBarInit()
  147. {
  148.     Handle    myMenuBar;
  149.     
  150.     myMenuBar = GetNewMBar (BASE_RES_ID);
  151.     SetMenuBar (myMenuBar);
  152.     gAppleMenu = GetMHandle (APPLE_MENU_ID);
  153.     AddResMenu (gAppleMenu, 'DRVR');
  154.     gFileMenu = GetMHandle (FILE_MENU_ID);
  155.     DrawMenuBar();
  156. }
  157.  
  158. /******************** SetUpDragRect *******************/
  159.  
  160. SetUpDragRect()
  161. {
  162.     gDragRect = screenBits.bounds;
  163.     gDragRect.left += DRAG_THRESHOLD;
  164.     gDragRect.right -= DRAG_THRESHOLD;
  165.     gDragRect.bottom -= DRAG_THRESHOLD;
  166. }
  167.  
  168. /******************** NotifyInit ***********************/
  169.  
  170. NotifyInit()
  171. {
  172.     gNotifyStrH = GetString (BASE_RES_ID);
  173.     gMyNMRec.qType = nmType;
  174.     gMyNMRec.nmMark = MARK_APPLICATION;
  175.     gMyNMRec.nmResp = NIL_POINTER;
  176. }
  177.  
  178. /******************** MainLoop ***************************/
  179.  
  180. MainLoop()
  181. {
  182.     gDone = FALSE;
  183.     gCounting = FALSE;
  184.     gNotify_set = FALSE;
  185.     
  186.     while (gDone == FALSE)
  187.     {
  188.         HandleEvent();
  189.     }
  190. }
  191.  
  192. /********************** HandleEvent ************************/
  193.  
  194. HandleEvent ()
  195. {
  196.     char theChar;
  197.     
  198.     WaitNextEvent (everyEvent, &gTheEvent, MIN_SLEEP,
  199.                                             NIL_MOUSE_REGION);
  200.     
  201.     switch (gTheEvent.what)
  202.     {
  203.         case mouseDown:
  204.             HandleMouseDown();
  205.             break;
  206.         case keyDown:
  207.         case autoKey:
  208.             theChar = gTheEvent.message & charCodeMask;
  209.             if (( gTheEvent.modifiers & cmdKey) != 0)
  210.                     HandleMenuChoice(MenuKey(theChar));
  211.             break;
  212.     }
  213. }
  214.  
  215. /************************ HandleMouse ******************/
  216.  
  217. HandleMouseDown()
  218. {
  219.     WindowPtr     whichWindow;
  220.     short int    thePart;
  221.     long int    menuChoice, windSize;
  222.     
  223.     thePart = FindWindow (gTheEvent.where, &whichWindow);
  224.     switch (thePart)
  225.     {
  226.         case inMenuBar:
  227.             menuChoice = MenuSelect (gTheEvent.where);
  228.             HandleMenuChoice(menuChoice);
  229.             break;
  230.         case inSysWindow:
  231.             SystemClick (&gTheEvent, whichWindow);
  232.             break;
  233.         case inDrag:
  234.             DragWindow(whichWindow,gTheEvent.where, &gDragRect);
  235.             break;
  236.         case inGoAway:
  237.             gDone = TRUE;
  238.             break;
  239.     }
  240. }
  241.  
  242. /***************** HandleMenuChoice ***************/
  243.  
  244. HandleMenuChoice(menuChoice)
  245. long int    menuChoice;
  246. {
  247.     int    theMenu;
  248.     int    theItem;
  249.     
  250.     if (menuChoice !=0)
  251.     {
  252.         theMenu = HiWord (menuChoice);
  253.         theItem = LoWord (menuChoice);
  254.         switch(theMenu)
  255.         {
  256.             case APPLE_MENU_ID:
  257.                 HandleAppleChoice(theItem);
  258.                 break;
  259.             case FILE_MENU_ID:
  260.                 HandleFileChoice(theItem);
  261.                 break;
  262.         }
  263.     }
  264.     HiliteMenu(0);
  265. }
  266.  
  267. /********************* HandleAppleChoice *************/
  268.  
  269. HandleAppleChoice(theItem)
  270. int    theItem;
  271. {
  272.     Str255        accName;
  273.     int            accNumber;
  274.     short int    itemNumber;
  275.     
  276.     switch (theItem)
  277.     {
  278.         case ABOUT_ITEM_ID:
  279.             NoteAlert(ABOUT_ALERT, NIL_POINTER);
  280.             break;
  281.         default:
  282.             GetItem(gAppleMenu,theItem,accName);
  283.             accNumber = OpenDeskAcc (accName);
  284.             break;
  285.     }
  286. }
  287.  
  288. /**************** HandleFileChoice ***********************/
  289.  
  290. HandleFileChoice (theItem)
  291. int    theItem;
  292. {
  293.     Str255    timeString;
  294.     long    countDownTime;
  295.     int        itemType;
  296.     Rect    itemRect;
  297.     Handle    itemHandle;
  298.     
  299.     switch (theItem)
  300.     {
  301.         case CHANGE_ITEM:
  302.             HandleDialog();
  303.             break;
  304.         case START_STOP_ITEM:
  305.             if (gCounting)
  306.             {
  307.                 SetItem (gFileMenu, theItem,"\pStart Countdown");
  308.                 gCounting = FALSE;
  309.             } else
  310.             {
  311.                 HiliteMenu(0);
  312.                 GetDItem (gSettingsDialog, TIME_FIELD,
  313.                         &itemType, &itemHandle, &itemRect);
  314.                 GetIText (itemHandle, &timeString);
  315.                 StringToNum (timeString, &countDownTime);
  316.                 
  317.                 DisableItem (gFileMenu, CHANGE_ITEM);
  318.                 SetItem (gFileMenu, theItem, "\pStop Countdown");
  319.                 CountDown (countDownTime);
  320.                 EnableItem (gFileMenu, CHANGE_ITEM);
  321.                 SetItem (gFileMenu, theItem, "\pStart Countdown");
  322.             }
  323.             break;
  324.         case KILL_ITEM:
  325.             NMRemove (&gMyNMRec);
  326.             HUnlock (gNotifyStrH);
  327.             DisableItem (gFileMenu, KILL_ITEM);
  328.             gNotify_set = FALSE;
  329.             break;
  330.         case QUIT_ITEM:
  331.             gCounting = FALSE;
  332.             gDone = TRUE;
  333.             break;
  334.     }
  335. }
  336.  
  337. /****************** HandleDialog **************************/
  338.  
  339. HandleDialog()
  340. {
  341.     int        itemHit, dialogDone = FALSE;
  342.     long    alarmDelay;
  343.     Str255    delayString;
  344.     int        itemType;
  345.     Rect    itemRect;
  346.     Handle    itemHandle;
  347.     
  348.     ShowWindow (gSettingsDialog);
  349.     SaveSettings();
  350.     
  351.     while (dialogDone == FALSE)
  352.     {
  353.         ModalDialog (NIL_POINTER, &itemHit);
  354.         switch (itemHit)
  355.         {
  356.             case SAVE_BUTTON:
  357.                 HideWindow (gSettingsDialog);
  358.                 dialogDone = TRUE;
  359.                 break;
  360.             case CANCEL_BUTTON:
  361.                 HideWindow (gSettingsDialog);
  362.                 RestoreSettings();
  363.                 dialogDone = TRUE;
  364.                 break;
  365.             case SOUND_ON_BOX:
  366.                 GetDItem (gSettingsDialog, SOUND_ON_BOX,
  367.                         &itemType, &itemHandle, &itemRect);
  368.                 SetCtlValue (itemHandle, !GetCtlValue(itemHandle));
  369.                 break;
  370.             case ICON_ON_BOX:
  371.                 GetDItem (gSettingsDialog, ICON_ON_BOX,
  372.                         &itemType, &itemHandle, &itemRect);
  373.                 SetCtlValue (itemHandle, !GetCtlValue(itemHandle));
  374.                 break;
  375.             case ALERT_ON_BOX:
  376.                 GetDItem (gSettingsDialog, ALERT_ON_BOX,
  377.                         &itemType, &itemHandle, &itemRect);
  378.                 SetCtlValue (itemHandle, !GetCtlValue(itemHandle));
  379.                 break;
  380.             case SECS_RADIO:
  381.                 gSeconds_or_minutes = SECONDS;
  382.                 GetDItem (gSettingsDialog, MINS_RADIO,
  383.                         &itemType, &itemHandle, &itemRect);
  384.                 SetCtlValue (itemHandle, OFF);
  385.                 GetDItem (gSettingsDialog, SECS_RADIO,
  386.                         &itemType, &itemHandle, &itemRect);
  387.                 SetCtlValue (itemHandle, ON);
  388.                 GetDItem (gSettingsDialog, S_OR_M_FIELD,
  389.                         &itemType, &itemHandle, &itemRect);
  390.                 SetIText (itemHandle, "\pseconds");
  391.                 GetDItem (gSettingsDialog, TIME_FIELD,
  392.                         &itemType, &itemHandle, &itemRect);
  393.                 HLock (gDefaultSecsH);
  394.                 SetIText (itemHandle, *gDefaultSecsH);
  395.                 HUnlock (gDefaultSecsH);
  396.                 break;
  397.             case MINS_RADIO:
  398.                 gSeconds_or_minutes = MINUTES;
  399.                 GetDItem (gSettingsDialog, SECS_RADIO,
  400.                         &itemType, &itemHandle, &itemRect);
  401.                 SetCtlValue (itemHandle, OFF);
  402.                 GetDItem (gSettingsDialog, MINS_RADIO,
  403.                         &itemType, &itemHandle, &itemRect);
  404.                 SetCtlValue (itemHandle, ON);
  405.                 GetDItem (gSettingsDialog, S_OR_M_FIELD,
  406.                         &itemType, &itemHandle, &itemRect);
  407.                 SetIText (itemHandle, "\pminutes");
  408.                 GetDItem (gSettingsDialog, TIME_FIELD,
  409.                         &itemType, &itemHandle, &itemRect);
  410.                 HLock (gDefaultMinsH);
  411.                 SetIText (itemHandle, *gDefaultMinsH);
  412.                 HUnlock (gDefaultMinsH);
  413.                 break;
  414.         }
  415.     }
  416. }
  417.  
  418. /************************ Save Setting ********************/
  419.  
  420. SaveSettings()
  421. {
  422.     int        itemType;
  423.     Rect    itemRect;
  424.     Handle    itemHandle;
  425.     
  426.     GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle,
  427.                                                     &itemRect);
  428.     GetIText (itemHandle, &(savedSettings.timeString));
  429.     GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle,
  430.                                                     &itemRect);
  431.     savedSettings.sound = GetCtlValue (itemHandle);
  432.     GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle,
  433.                                                     &itemRect);
  434.     savedSettings.icon = GetCtlValue (itemHandle);
  435.     GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle,
  436.                                                     &itemRect);
  437.     savedSettings.alert = GetCtlValue (itemHandle);
  438.     GetDItem (gSettingsDialog, SECS_RADIO, &itemType, &itemHandle,
  439.                                                     &itemRect);
  440.     savedSettings.secsRadio = GetCtlValue (itemHandle);
  441.     GetDItem (gSettingsDialog, MINS_RADIO, &itemType, &itemHandle,
  442.                                                     &itemRect);
  443.     savedSettings.minsRadio = GetCtlValue (itemHandle);
  444. }
  445.  
  446. /************************* RestoreSettings ********************/
  447.  
  448. RestoreSettings()
  449. {
  450.     int        itemType;
  451.     Rect    itemRect;
  452.     Handle    itemHandle;
  453.     
  454.     GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle,
  455.                                                     &itemRect);
  456.     SetIText (itemHandle, savedSettings.timeString);
  457.     GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle,
  458.                                                     &itemRect);
  459.     SetCtlValue (itemHandle, savedSettings.sound);
  460.     GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle,
  461.                                                     &itemRect);
  462.     SetCtlValue (itemHandle, savedSettings.icon);
  463.     GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle,
  464.                                                     &itemRect);
  465.     SetCtlValue (itemHandle, savedSettings.alert);
  466.     GetDItem (gSettingsDialog, SECS_RADIO, &itemType, &itemHandle,
  467.                                                     &itemRect);
  468.     SetCtlValue (itemHandle, savedSettings.secsRadio);
  469.     GetDItem (gSettingsDialog, MINS_RADIO, &itemType, &itemHandle,
  470.                                                     &itemRect);
  471.     SetCtlValue (itemHandle, savedSettings.minsRadio);
  472.  
  473.     if (savedSettings.secsRadio == ON)
  474.     {
  475.         GetDItem (gSettingsDialog, S_OR_M_FIELD, &itemType,
  476.                                             &itemHandle, &itemRect);
  477.         SetIText (itemHandle, "\pseconds");
  478.     } else
  479.     {
  480.         GetDItem (gSettingsDialog, S_OR_M_FIELD, &itemType,
  481.                                             &itemHandle, &itemRect);
  482.         SetIText (itemHandle, "\pminutes");
  483.     }
  484. }
  485.  
  486. /************************** Countdown ****************************/
  487.  
  488. CountDown (numSecs)
  489. long numSecs;
  490. {
  491.     long        myTime, oldTime, difTime;
  492.     Str255        myTimeString;
  493.     WindowPtr    countDownWindow;
  494.     
  495.     countDownWindow = GetNewWindow (BASE_RES_ID, NIL_POINTER, 
  496.                                                 MOVE_TO_FRONT);
  497.     SetPort (countDownWindow);
  498.     ShowWindow (countDownWindow);
  499.     TextFace (bold);
  500.     TextSize (24);
  501.     
  502.     GetDateTime (&myTime);
  503.     oldTime = myTime;
  504.     if (gSeconds_or_minutes == MINUTES)
  505.             numSecs *= SECONDS_PER_MINUTE;
  506.     gCounting = TRUE;
  507.     
  508.     while ((numSecs > 0) && (gCounting))
  509.     {
  510.         HandleEvent();
  511.         if (gCounting)
  512.         {
  513.             MoveTo (LEFT, TOP);
  514.             GetDateTime (&myTime);
  515.             if (myTime != oldTime)
  516.             {
  517.                 difTime = myTime - oldTime;
  518.                 numSecs = numSecs - difTime;
  519.                 oldTime = myTime;
  520.                 NumToString (numSecs, myTimeString);
  521.                 EraseRect ( &(countDownWindow->portRect));
  522.                 DrawString (myTimeString);
  523.             }
  524.         }
  525.     }
  526.     if (gCounting)
  527.             SetNotification();
  528.         gCounting = FALSE;
  529.         HideWindow (countDownWindow);
  530. }
  531.  
  532. /************************* SetNotification *******************/
  533.  
  534. SetNotification()
  535. {
  536.     int        itemType;
  537.     Rect    itemRect;
  538.     Handle    itemHandle;
  539.     
  540.     if (gNotify_set)
  541.     {
  542.         NMRemove (&gMyNMRec);
  543.         HUnlock (gNotifyStrH);
  544.     }
  545.     
  546.     GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle,
  547.                                                     &itemRect);
  548.     if (GetCtlValue (itemHandle))
  549.             gMyNMRec.nmSIcon = GetResource ('SICN', BASE_RES_ID);
  550.     else
  551.             gMyNMRec.nmSIcon = NIL_POINTER;
  552.             
  553.     GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle,
  554.                                                     &itemRect);
  555.     if (GetCtlValue (itemHandle))
  556.             gMyNMRec.nmSound = GetResource ('snd ', BASE_RES_ID);
  557.     else
  558.             gMyNMRec.nmSound = NIL_POINTER;
  559.     
  560.     GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle,
  561.                                                     &itemRect);
  562.     if (GetCtlValue(itemHandle))
  563.     {
  564.         MoveHHi (gNotifyStrH);
  565.         HLock (gNotifyStrH);
  566.         gMyNMRec.nmStr = *gNotifyStrH;
  567.     }
  568.     else
  569.         gMyNMRec.nmStr = NIL_POINTER;
  570.         
  571.     NMInstall (&gMyNMRec);
  572.     EnableItem (gFileMenu, KILL_ITEM);
  573.     gNotify_set = TRUE;
  574. }
  575.  
  576.         
  577.  
  578.  
  579.  
  580.  
  581.             
  582.                 
  583.